/*
 * 
 *  This file is part of Sourceprojects.org Lycia Copyright 2010 noctarius
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 * 
 */
package org.sourceprojects.lycia.internal.generator;

import org.sourceprojects.lycia.Generator;
import org.sourceprojects.lycia.GeneratorFactory;
import org.sourceprojects.lycia.GenerationStrategy;
import org.sourceprojects.lycia.exceptions.LyciaConfigurationException;
import org.sourceprojects.lycia.internal.platform.PlatformSupportUtils;

/**
 * 
 * @author noctarius
 * @since 1.9.1
 * 
 * @param <B>
 */
public class GeneratorFactoryImpl<T> implements GeneratorFactory<T> {
	private static final String REFLECTIVE_GENERATOR = "org.sourceprojects.lycia.internal.generator.ReflectiveGenerator";
	private static final String BYTECODE_GENERATOR = "org.sourceprojects.lycia.internal.generator.BytecodeGenerator";

	private Class<? extends Generator<?>> CUSTOM_GENERATOR_CLASS = null;

	public GeneratorFactoryImpl() {
	}

	@Override
	@SuppressWarnings("unchecked")
	public final Generator<T> newInstance(
			final GenerationStrategy generationStrategy) {
		try {
			Class<? extends Generator<T>> clazz;

			switch (generationStrategy) {
			case BytecodeGeneration:
				if (!PlatformSupportUtils.isBytecodeGenerationAvailable())
					throw new LyciaConfigurationException(
							"Bytecode-Generation strategy was choosen but "
									+ "ASM wasn't found on the classpath");

				clazz = (Class<? extends Generator<T>>) Class
						.forName(BYTECODE_GENERATOR);
				break;

			case ReflectiveGeneration:
				clazz = (Class<? extends Generator<T>>) Class
						.forName(REFLECTIVE_GENERATOR);
				break;

			case Customized:
				if (CUSTOM_GENERATOR_CLASS != null) {
					clazz = (Class<? extends Generator<T>>) CUSTOM_GENERATOR_CLASS;
					break;
				}

				//$FALL-THROUGH$
			default:
				throw new LyciaConfigurationException(
						"GenerationStrategy is not yet supported");
			}

			return clazz.newInstance();
		} catch (final ClassNotFoundException e) {
			throw new LyciaConfigurationException(
					"GeneratorStrategy could not be initialized", e);
		} catch (final InstantiationException e) {
			throw new LyciaConfigurationException(
					"GeneratorStrategy could not be initialized", e);
		} catch (final IllegalAccessException e) {
			throw new LyciaConfigurationException(
					"GeneratorStrategy could not be initialized", e);
		}
	}

	@Override
	public final void setCustomGeneratorClass(
			final Class<? extends Generator<T>> clazz) {
		CUSTOM_GENERATOR_CLASS = clazz;
	}
}
